package com.bawei.park2211a.adapter

import android.graphics.Color
import android.util.Log
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.bawei.base.Res
import com.bawei.base.bean.DepartmentIdEntity
import com.bawei.base.bean.StaffEntity
import com.bawei.base.bean.VisitEntity
import com.bawei.base.net.ApiService
import com.bawei.park2211a.R
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import kotlinx.coroutines.launch
import javax.inject.Inject

class VisitAdapter @Inject constructor(var apiService: ApiService, private val lifecycleOwner: LifecycleOwner) :BaseQuickAdapter<VisitEntity,BaseViewHolder>(R.layout.item_visit) {
    override fun convert(helper: BaseViewHolder, item: VisitEntity) {
        var staff: Res<StaffEntity>
        var departmentId: Res<DepartmentIdEntity>
        helper.setText(R.id.visit_time,item.appointmentTime)
        helper.setText(R.id.visit_name,item.visitorName)
        helper.setText(R.id.visit_phone,item.phoneNumber)
        helper.setText(R.id.visit_matter,"来访事由：${item.purpose}")
        val view = helper.getView<TextView>(R.id.visir_statu)
        view.text=item.visitStatus
        when(item.visitStatus){
            "已通过"->{
                view.setBackgroundResource(R.drawable.text_color1)
                view.setTextColor(Color.GREEN)
            }
            "驳回"->{
                view.setBackgroundResource(R.drawable.text_color6)
                view.setTextColor(Color.WHITE)
            }
            "待审核"->{
                view.setBackgroundResource(R.drawable.text_color3)
                view.setTextColor(Color.WHITE)
            }
            "已完成"->{
                view.setBackgroundResource(R.drawable.text_color2)
                view.setTextColor(Color.BLUE)
            }
        }
        lifecycleOwner.lifecycleScope.launch {
             try {
                 staff = apiService.staff(item.employeeId)
                 departmentId=apiService.departmentId(staff.data.departmentId)
                 if (item.requestParking){
                     helper.setText(R.id.visit_person,"访问 ${staff.data.name} (${departmentId.data.name})")
                 }else{
                     helper.setText(R.id.visit_area,"未申请访车位")
                 }
             } catch (e: Exception) {
                 helper.setText(R.id.visit_person,"没有找到访问人")
             }
        }


    }
}